#include <Arduino.h>
#include <Wire.h>
#include <WiFi.h>
#include <DHT.h>
#include <HttpClient.h>
#include <MQUnifiedsensor.h>
#include <ESP32AnalogRead.h>
ESP32AnalogRead adc;


const char *ssid = "test";//WIFI
const char *password = "123456789";//WIFI密码
const char* serverIP = "183.230.40.33"; //欲访问的地址
uint16_t serverPort = 80;         //服务器端口号
String url = "http://api.heclouds.com/devices/798051703/datapoints?type=3";//网址 设备ID
String api="PTpjtG8AifjsP6CaiU5yKrKtCCU=";//api-key
String post;//http请求
int Content_Length;
WiFiClient client; //声明一个客户端对象，用于与服务器进行连接

uint16_t clk;
#define CAL_PPM 20  // 
#define RL			5		// RL
static float ADC_num, R0; //

#define LED_BUILTIN   02
#define SMOKE         32
#define BEEP          12

//long  smoke = 0;
float smoke = 0;
int8_t beep_flag = 0;

void MQ2_PPM_Calibration(float RS)
{
    R0 = RS / pow(CAL_PPM / 613.9f, 1 / -2.074f);
}

float MQ2_GetPPM(void)
{

    float Vrl = ADC_num;
    float RS = (3.3f - Vrl) / Vrl * RL;
    if(clk < 10)
    {
        MQ2_PPM_Calibration(RS);
    }
    else clk = 10;

    float ppm = 613.9f * pow(RS/R0, -2.074f);
    return  ppm;
}

void setup(void)
{
	Serial.begin(115200);

    //PIN CFG
    pinMode(BEEP, OUTPUT);
    digitalWrite(BEEP, 1); //low - beep

    //WIFI
    Serial.print("Connecting to ");
    Serial.println(ssid);
    WiFi.begin(ssid, password);//连接到网络

   while (WiFi.status() != WL_CONNECTED) {//等待网络连接成功
        delay(500);
        Serial.print(".");
    }

    Serial.println("");
    Serial.println("WiFi connected");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());//打印模块IP
}

void loop(void)
{
    byte i;
    byte present = 0;
    byte type_s;
    byte data[12];
    byte addr[8];
    float celsius, fahrenheit;

    
    //LED
    digitalWrite(LED_BUILTIN, HIGH);
    delay(500);
    digitalWrite(LED_BUILTIN, LOW);
    delay(500);

    adc.attach(SMOKE);
    ADC_num = adc.readVoltage();
    Serial.println("Voltage = "+String(ADC_num));
    clk++;
    smoke = MQ2_GetPPM();
    Serial.println("smoke = "+String(smoke));

    if(smoke > 1000)
        digitalWrite(BEEP, 0);
    else digitalWrite(BEEP, 1);
   
    if (!client.connect(serverIP, serverPort)) {//判断是否连接服务器
        Serial.println("connection failed");
        return;
    }else{//连接成功
       //Serial.println("connection succ");
       //Serial.println();
       String data_value="{\"smoke_value\":"+String(smoke)+"}\r\n\r\n";


       Content_Length=data_value.length();
       post=String("POST ")+url+" HTTP/1.1\r\n"+"api-key: "+api+"\r\n"
          +"Host:api.heclouds.com\r\n"+"Connection:close\r\n"+"Content-Length:"
          +String(Content_Length)+"\r\n\r\n"+data_value;
          
        // Serial.println(Content_Length);
        Serial.print(post);//串口打印HTTP请求
        client.print(post);//向服务器发送HTTP请求
      }
       while(client.available()) {//判断WIFI是否有数据
        Serial.print(post);
        String line = client.readStringUntil('\r');//读取WIFI数据赋给line
        Serial.print(line);//打印line 
    }
}